﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Station.Inspect.UI
{
    public partial class FormMark : Form
    {
        NodeMark Mark { get; set; }
        public FormMark(NodeMark node)
        {
            InitializeComponent();
            this.Mark = node;
            if (node != null)
            {
                tbName.Text = node.Name;
                var rect = node.Rect;
                var pt = StaInspect.Instance.Rbt.P2TPByOrig(new Point(rect.X + rect.Width / 2, rect.Y + rect.Height / 2));
                tbLocation.Text = pt.ToString();
                tbdX.Text = node.Dx.ToString();
                tbdY.Text = node.Dy.ToString();
                tbThreshold.Text = node.Threshold.ToString();
                tbAngleLimit.Text = node.AngleLimit.ToString();
                pbImg.Image = node.Image;
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            if (Mark != null)
            {
                Mark.Dx = int.Parse(tbdX.Text);
                Mark.Dy = int.Parse(tbdY.Text);
                Mark.Threshold = double.Parse(tbThreshold.Text);
                Mark.AngleLimit = double.Parse(tbAngleLimit.Text);
                for (int i = 0; i < StaInspect.Instance.Prj.Marks.Count; i++)
                {
                    if (StaInspect.Instance.Prj.Marks[i].ID == Mark.ID)
                    {
                        StaInspect.Instance.Prj.Marks[i] = Mark;
                       string path = StaInspect.Instance.loadPath + "\\Mark\\" + StaInspect.Instance.Prj.FaceName;
                        Directory.CreateDirectory(path);
                        string fileName = path + $"\\{Mark.ID}.png";
                        if (File.Exists(fileName))
                            File.Delete(fileName);
                        Mark.Image?.Save(fileName);
                    }
                       

                }
            }
        }

        private void FormMark_Load(object sender, EventArgs e)
        {

        }
    }
}
